package com.lagou.web.servlet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SimplePropertyPreFilter;
import com.lagou.base.BaseServlet;
import com.lagou.pojo.Course;
import com.lagou.pojo.Course_Lesson;
import com.lagou.pojo.Course_Section;
import com.lagou.service.CourseContentService;
import com.lagou.service.impl.CourseContentServiceImpl;
import com.lagou.utils.DateUtils;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author : liuchangling
 * @Descrition :
 * @Date： Created in 2:03 下午 2021/6/11
 */
@WebServlet("/courseContent")
public class CourseContentServlet extends HttpServlet {
    CourseContentService courseContentService = new CourseContentServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String methodName = req.getParameter("methodName");
        switch (methodName) {
            case "findSectionAndLessonByCourseId":
                findSectionAndLessonByCourseId(req, resp);
                break;
            case "findCourseById":
                findCourseById(req, resp);
                break;
            case "updateSectionStatus":
                updateSectionStatus(req, resp);
                break;
            default:
                break;
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        StringBuilder sb = new StringBuilder();
        try {
            BufferedReader streamReader = new BufferedReader(new InputStreamReader(req.getInputStream(), "UTF-8"));
            String inputStr;
            while ((inputStr = streamReader.readLine()) != null) {
                sb.append(inputStr);
            }
            System.out.println(sb.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
        Map map = (Map) JSON.parse(sb.toString());
        String methodName = map.get("methodName").toString();
        System.out.println(methodName);
        switch (methodName) {
            case "saveOrUpdateLesson":
                saveOrUpdateLesson(map, resp);
                break;
            case "saveOrUpdateSection":
                saveOrUpdateSection(map, resp);
                break;
            default:
                break;
        }
    }


    public void findSectionAndLessonByCourseId(HttpServletRequest request, HttpServletResponse response) {
        //接收参数
        try {
            int id = Integer.parseInt(request.getParameter("course_id"));

            List<Course_Lesson> lessonList = courseContentService.findLessonByCourseId(id);

            List<Course_Section> sectionList = courseContentService.findSectionByCourseId(id);
            Map<String, Object> map = new HashMap<>();
            map.put("lessonList", lessonList);

            String result = JSON.toJSONString(map) + JSON.toJSONString(sectionList);
            response.getWriter().print(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void findCourseById(HttpServletRequest request, HttpServletResponse response) {
        try {
            int course_id = Integer.parseInt(request.getParameter("course_id"));
            Course course = courseContentService.findCourseById(course_id);
            SimplePropertyPreFilter filter = new SimplePropertyPreFilter(Course.class, "id", "course_name");
            String result = JSON.toJSONString(course, filter);
            response.getWriter().print(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void saveOrUpdateSection(Map map, HttpServletResponse response) {
        Course_Section courseSection = new Course_Section();
        try {
            BeanUtils.populate(courseSection, map);

            if (map.get("id") != null && map.get("id") != "0") {
                String result = courseContentService.updateSection(courseSection);
                response.getWriter().print(result);
            } else {
                String result = courseContentService.saveSection(courseSection);
                response.getWriter().print(result);
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void updateSectionStatus(HttpServletRequest request, HttpServletResponse response) {
        try {
            Course_Section courseSection = new Course_Section();
            String id = request.getParameter("id");
            int status = Integer.parseInt(request.getParameter("status"));
            courseSection.setStatus(status);
            if (id != null) {
                courseSection.setId(Integer.parseInt(id));
            }
            courseSection.setUpdate_time(DateUtils.getDateFormart());
            String s = courseContentService.updateCourse_SectionStatus(courseSection);
            response.getWriter().print(s);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public void saveOrUpdateLesson(Map map, HttpServletResponse response) {
        Course_Lesson courseLesson = new Course_Lesson();
        try {
            BeanUtils.populate(courseLesson, map);
            if (map.get("id") != null && !map.get("id").equals("0")) {
                String result = courseContentService.updateLesson(courseLesson);
                response.getWriter().print(result);
            } else {
                String result = courseContentService.saveLesson(courseLesson);
                response.getWriter().print(result);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
